<template>
	<view class="wrap safe-area flex column align-center">
		<view class="wrap-white-bg">
			<block v-for="(item, index) in list" :key="index">
				<rowInput
					v-if="item.type === 'input' && item.direct === 'row'"
					:item="item"
					v-model="dataInfo[item.key]"
				/>
				
				<rowUpload
					v-else-if="item.type === 'upload' && item.direct === 'row'"
					:item="item"
					v-model="dataInfo[item.key]"
				/>
				
				<rowSwitch 
					v-else-if="item.type === 'switch' && item.direct === 'row'"
					:item="item"
					v-model="dataInfo[item.key]"
				/>
			</block>
		</view>
		
		<view class="sure-btn theme-gradient-btn f-34" @click="onSubmit">提交</view>
	</view>
</template>

<script>
	import {createShopGroup} from "@/API/store.js";
	import rowInput from "@/components/form/rowInput.vue";
	import rowUpload from "@/components/form/rowUpload.vue";
	import rowSwitch from "@/components/form/rowSwitch.vue";
	export default {
		components: {
			rowInput,
			rowUpload,
			rowSwitch
		},
		data() {
			return {
				dataInfo: {
					group_name: "",
					goup_image: "",
					group_code: "",
					group_manager: "",
					has_group: "1",
				},
			}
		},
		computed: {
			list() {
				return [
					{
						title: "群名称",
						require: true,
						type: "input",
						key: "group_name",
						place: "请输入群名称",
						mode: "text",
						maxlength: 100,
						tips: "请输入群名称",
						direct: "row",
					},
					{
						title: "群头像",
						require: true,
						type: "upload",
						key: "goup_image",
						direct: "row",
						tips: "请上传群头像",
					},
					{
						title: "群二维码",
						require: true,
						type: "upload",
						key: "group_code",
						direct: "row",
						tips: "请上传群二维码",
					},
					{
						title: "管理者微信",
						require: true,
						type: "input",
						key: "group_manager",
						place: "请输入管理者微信",
						mode: "text",
						maxlength: 100,
						tips: "请输入管理者微信",
						direct: "row",
					},
					{
						title: "是否关闭",
						require: true,
						type: "switch",
						key: "has_group",
						direct: "row",
					}
				]
			}
		},
		onLoad(options) {
			if (options.dataInfo) {
				const before = JSON.parse(options.dataInfo);
				if (before.group_code !== null) {
					Object.keys(this.dataInfo).forEach((key) => {
						this.dataInfo[key] = before[key];
					})
				}
			}
		},
		methods: {
			/**
			 * 提交
			 */
			onSubmit() {
				if (this.$filters.judgeFormData(this.list, this.dataInfo)) {
					this.$throttle(() => {
						let obj = {...this.dataInfo};
						
						createShopGroup(obj).then(res => {
							this.$st.success("修改成功", () => {
								uni.navigateBack();
							})
						})
					});
				}
			},
		}
	}
</script>

<style>
	@import "@/css/publish.css";
</style>